﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace arma3server
{
    public partial class arma3server : Form
    {
        public arma3server()
        {
            InitializeComponent();
        }

        private void arma3server_Load(object sender, EventArgs e)
        {
            var args = Environment.GetCommandLineArgs();
            var name = "public";
            var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            var path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            var pidpath = "";
            foreach (var arg in args)
            {
                if (arg.ToLowerInvariant().StartsWith("-name="))
                {
                    name = (arg.Split(new char[]{'='})[1]).Replace("\"","").Trim();
                }
                if (arg.ToLowerInvariant().StartsWith("-profiles="))
                {
                    path = (arg.Split(new char[] { '=' })[1]).Replace("\"", "").Trim();
                }
                //-pid={{server_config_path}}{{server_name}}.pid
                if (arg.ToLowerInvariant().StartsWith("-pid="))
                {
                    pidpath = (arg.Split(new char[] { '=' })[1]).Replace("\"", "").Trim();
                }
                this.Text += " " + arg;
                this.txtBox.Text += arg + Environment.NewLine;
            }
            this.txtBox.Select(0, 0);
            if (pidpath == "")
            {
                pidpath = System.IO.Path.Combine(path, name + ".pid");
            }
            System.IO.File.WriteAllText(pidpath, currentProcess.Id.ToString());
        }

    }
}
